/**
 * Created by Admin on 2018/1/4.
 */
define(function (require, exports, module) {
    var template = require("template");
    var loadingTpl = require("../common/tpl/loading.html");
    var yearTpl = require("./tpl/trendYearTpl.html");
    var monthTpl = require("./tpl/trendMonthTpl.html");
    require("../common/highchart/highcharts");
    require("../common/highchart/linechart");
    require("../common/highchart/piechartS");
    require("../common/qy-template");

    var analysis = {
        initTable: function (trendType, yearItem) {
            $("#validptchart-tab").html(loadingTpl);
            var url, title;
            if (!yearItem) {
                url = window.jkconfig.webapiurlv + "analysis/trendanalysis/" + trendType;
                title = (trendType == 0 ? "申请" : "授权") + "专利年度统计";
            } else {
                url = window.jkconfig.webapiurlv + "analysis/trenddetail/" + trendType + "?year=" + yearItem;
                title = (trendType == 0 ? "申请" : "授权") + "专利月度统计";
                $(".yearBox").html(yearItem.toString() + '年度');
            }
            $.ajax({
                url: url,
                success: function (data) {
                    $(".la-square-jelly-box.la-2x").hide();
                    if (data.code == 0 && data.infos != null) {
                        var tpl = typeof yearItem == "undefined" ? yearTpl : monthTpl;
                        var html = template.compile(tpl)({data: data, type: trendType})
                        $("#validptchart-tab").html(html);
                        /*遍历info,得到四个数组：year=[],fm=[],sy=[],wg=[]*/
                        for (var i = data.infos.length - 1, year = [], fmcount = [], syxxcount = [],
                                 wgsjcount = []; i >= 0; i--) {
                            year.push(data.infos[i].year);
                            fmcount.push(data.infos[i].fmcount);
                            syxxcount.push(data.infos[i].syxxcount);
                            wgsjcount.push(data.infos[i].wgsjcount);
                        }

                        var info = "统计本区域内十年来" + (trendType == 0 ? "历年申请" : "历年授权") + "专利的数量,可以按年/月查看。。"
                        var sourceData = [
                            {name: '发明专利', data: fmcount},
                            {name: '实用新型', data: syxxcount},
                            {name: '外观专利', data: wgsjcount}
                        ];
                        //线形图初始化
                        var linechart = new LineChart({
                            dom: "ptamountchart",
                            xData: year,
                            yData: sourceData,
                            linetitle: title,
                            linetitleD: info,
                            linetitleDClass: "p-t-question-chart"
                        });

                        var totalA = [];
                        $.each($(".totaleC"), function () {
                            totalA.push($(this).text());
                        });
                        if (typeof(totalA[0]) == "undefined") {
                            totalA[0] = 0;
                        }
                        piedata = [];
                        for (var i = 0; i < data.infos.length; i++) {
                            /*判断发明、外观、实用新型专利的数量不为0*/
                            var middleData = [];
                            middleData[0] = {name: '发明专利', y: data.infos[i].fmcount};
                            middleData[1] = {name: '实用新型', y: data.infos[i].syxxcount};
                            middleData[2] = {name: '外观专利', y: data.infos[i].wgsjcount};
                            piedata.push(middleData);
                        }

                        var piedtitle = "当前有效专利",
                            pietitle = piedtitle,
                            subtitle = "共" + totalA[0] + "件",
                            newPieChart = new PieChart({//饼图初始化
                                dom: "areaChart",
                                piedata: piedata,
                                pietitle: pietitle,//饼图一级标题
                                subtitle: subtitle,//饼图二级标题，
                            }),
                            valChartTab = $("#validptchart-tab tr");//饼状图改变容器
                        var pieChange = new PieChange(valChartTab, false, true, newPieChart, totalA, pietitle, "");
                    } else {
                        $(".nodata").show();
                        $("#validptchart-tab").hide();
                        var html = "<div style='width:100%;height:400px;font-size:20px;line-height:400px;text-align:center'>暂无数据</div>"
                        $(".pt16 .statistical-layout").html(html);
                    }
                }
            });
            $("body").on("click", ".btnExport", function () {
                if (typeof yearItem == "undefined") {
                    window.location.href = window.jkconfig.webapiurlv + "analysisexcel/trendanalysis/" + trendType + "?" + api_key + "=" + token;
                } else {
                    window.location.href = window.jkconfig.webapiurlv + "analysisexcel/trenddetail/" + trendType + "?year=" + yearItem + "&" + api_key + "=" + token;
                }
            })
            $("body").on('click','[data-fm]',function () {
                localStorage.setItem("patHubTypeName", JSON.stringify(['发明公开', '发明授权']));
                commonSet($(this))
            });
            $(document).on('click','[data-sy]',function () {
                localStorage.setItem("patHubTypeName", JSON.stringify(['实用新型']));
                commonSet($(this))
            });
            $(document).on('click','[data-wg]',function () {
                localStorage.setItem("patHubTypeName", JSON.stringify(['外观设计']));
                commonSet($(this))
            });
            $(document).on('click','[data-total]',function () {
                commonSet($(this))
            });
            
            function commonSet($td) {
                var year= $td.parent('tr').children('td').eq(1).find('a').html()
                // localStorage.setItem("lawState", JSON.stringify([1]));
                localStorage.setItem("patAppDateBegin", year.toString() + '-01-01');
                localStorage.setItem("patAppDateEnd", year.toString() + '-12-31');
                window.top.location.href = "/index.html#/analysis/patSearch.html";
            }
        },
    }
    module.exports = analysis;
})